#ifndef TESTFUN_H
#define TESTFUN_H
#include <QApplication>
#include <QCoreApplication>

#include "ui_mainwidget.h"
#include "usermanager.h"
#include <QThread>

#include "itemmanager.h"
#include "methodmanager.h"
#include "loadingwidget.h"
#include "QDebug"
#include "login.h"
#include "mainwidget.h"
#include "QScreen"


//注册示例，返回bool
void registerUser(){
    UserManager userManager;
    //输入用户命和密码,注册成功则返回true，失败false
    bool registrationResult = userManager.createUser("YOYO1", "123123");
    if (registrationResult) {
        qDebug() << "Registration successful";
    } else {
        qDebug() << "Registration failed";
    }
}

//登陆示例，登陆成功返回user结构体
void loginUser()
{
    UserManager userManager;
    QString username = "yoyo";
    QString password = "123123"; // 替换为对应的密码

    // 检查用户是否存在
    if (!userManager.checkUserExists(username)) {
        qDebug() << "用户" << username << "不存在";
        return;
    }

    QString TruelastLoginTime = userManager.getLastLoginTime(username); // 保存旧的上次登录时间

    // 输入用户名和密码，登录成功则返回 true，失败则返回 false
    User user = userManager.login(username, password);

    if (!user.username.isEmpty()) {
        qDebug() << "Login successful for user:" << user.username;
        qDebug() << "Last Login Time:" << TruelastLoginTime;
    } else {
        qDebug() << "Login failed for user:" << username;
    }
}

//获取全部角色,返回类型std::vector<UserManager::User>
std::vector<User> getAllUser(){
    UserManager userManager;
    std::vector<User> allUsers = userManager.getAllUsers();
    for (const User& user : allUsers) {
        qDebug() << "Username:" << user.username;
        qDebug() << "Password:" << user.password;
        qDebug() << "Last Login Time:" << user.lastLoginTime;
        qDebug() << "Permissions:" << user.permissions;
        qDebug() << "----------------------";
    }
    return allUsers;
}

//根据用户名删除用户
void delUser(){
    UserManager userManager;
    bool deletUserResult = userManager.deleteUser("yoyo");
    if(deletUserResult){
        qDebug()<<"delete success";
    }else{
        qDebug()<<"delete failed";
    }
}

//传入item结构体即可，创建item
void testCreateItem()
{
    ItemManager itemManager;

    Item item;
    item.barcode = "123456780";
    item.itemName = "Sample Item";
    item.methodName = "Sample Method";
    item.lightIntensity = 100;
    item.integrationTime = 10;
    item.integrationCount = 5;
    item.displacement = {1, 2, 3};
    item.createdBy = "yoyo";

    bool result = itemManager.createItem(item);
    if (result) {
        qDebug() << "Item created successfully";
    } else {
        qDebug() << "Failed to create item";
    }
}

//输出全部item
void testGetAllItems()
{
    ItemManager itemManager;

    QVector<Item> allItems = itemManager.getAllItems();
    for (const Item& item : allItems) {
        qDebug() << "Barcode: " << item.barcode;
        qDebug() << "Item Name: " << item.itemName;
        qDebug() << "Method Name: " << item.methodName;
        qDebug() << "Light Intensity: " << item.lightIntensity;
        qDebug() << "Integration Time: " << item.integrationTime;
        qDebug() << "Integration Count: " << item.integrationCount;
        qDebug() << "Displacement: ";
        for (int value : item.displacement) {
            qDebug() << value << " ";
        }
        qDebug() << "Created By: " << item.createdBy;
        qDebug() << "----------------------";
    }
}

//删除指定barcode的item
void testDeleteItem()
{
    ItemManager itemManager;

    QString barcode = "123456789";
    bool result = itemManager.deleteItem(barcode);
    if (result) {
        qDebug() << "Item deleted successfully" ;
    } else {
        qDebug() << "Failed to delete item" ;
    }
}

//删除生成用户为某一个人的全部item
void testDeleteItemsByUser()
{
    ItemManager itemManager;

    QString username = "John Doe";
    bool result = itemManager.deleteItemsByUser(username);
    if (result) {
        qDebug() << "Items deleted successfully" ;
    } else {
        qDebug() << "Failed to delete items" ;
    }
}

//查询item的信息
void testGetItem()
{
    ItemManager itemManager;

    QString barcode = "123456789"; // 替换为要查询的条形码

    Item item = itemManager.getItem(barcode);

    if (!item.barcode.isEmpty()) {
        qDebug() << "Item found:";
        qDebug() << "Barcode:" << item.barcode;
        qDebug() << "Item Name:" << item.itemName;
        qDebug() << "Method Name:" << item.methodName;
        qDebug() << "Creation Time:" << item.creationTime.toString();
        qDebug() << "Created By:" << item.createdBy;
        qDebug() << "Light Intensity:" << item.lightIntensity;
        qDebug() << "Integration Time:" << item.integrationTime;
        qDebug() << "Integration Count:" << item.integrationCount;
        qDebug() << "Displacement:";
        for (int value : item.displacement) {
            qDebug() << value;
        }
    } else {
        qDebug() << "Item not found";
    }
}

//带权限（即必须用户只能查看自己的item信息）的item查询
void testGetItemByUser()
{
    ItemManager itemManager;

    // 测试存在的物品
    QString barcode = "123456780";
    QString username = "yoyo";
    Item item = itemManager.getItemByUser(barcode, username);
    qDebug() << "Item Exists:";
    qDebug() << "Barcode:" << item.barcode;
    qDebug() << "Item Name:" << item.itemName;
    qDebug() << "Method Name:" << item.methodName;
    qDebug() << "Creation Time:" << item.creationTime.toString();
    qDebug() << "Created By:" << item.createdBy;
    qDebug() << "Light Intensity:" << item.lightIntensity;
    qDebug() << "Integration Time:" << item.integrationTime;
    qDebug() << "Integration Count:" << item.integrationCount;
    qDebug() << "Displacement:" << item.displacement;

    // 测试不存在的物品
    barcode = "67890";
    username = "User2";
    item = itemManager.getItemByUser(barcode, username);
    qDebug() << "Item Does Not Exist:";
    qDebug() << "Barcode:" << item.barcode;
}

//检测item是否存在
void testCheckItemExists()
{
    ItemManager itemManager;

    QString barcode = "123456780"; // 替换为要检索的物品名称

    bool exists = itemManager.checkItemExists(barcode);

    if (exists) {
        qDebug() << "Item" << barcode << "exists";
    } else {
        qDebug() << "Item" << barcode << "does not exist";
    }
}

//测试方法创建
void createMethodTest()
{
    MethodManager methodManager;

    // 创建方法
    Method method;
    method.methodName = "method2";
    method.createdBy = "YOYO1";
    method.lightIntensity = 100;
    method.integrationTime = 200;
    method.integrationCount = 3;
    method.displacement = {1, 2, 3, 4};

    bool createMethodResult = methodManager.createMethod(method);
    qDebug() << "Create Method Result:" << (createMethodResult ? "Success" : "Failure");
}

//获取全部方法，用vector返回
void getAllMethodTest()
{
    MethodManager methodManager;

    // 获取所有方法
    QVector<Method> allMethods = methodManager.getAllMethod();

    qDebug() << "All Methods:";
    for (const Method& method : allMethods) {
        qDebug() << "Method Name:" << method.methodName;
        qDebug() << "Created By:" << method.createdBy;
        qDebug() << "Light Intensity:" << method.lightIntensity;
        qDebug() << "Integration Time:" << method.integrationTime;
        qDebug() << "Integration Count:" << method.integrationCount;
        qDebug() << "Displacement:" << method.displacement;
    }
}

//根据用户名返回方法
void getMethodByUserTest(const QString& username)
{
    MethodManager methodManager;
    // 获取指定用户的方法
    QVector<Method> userMethods = methodManager.getMethodByUser(username);

    qDebug() << "Methods for User" << username << ":";
    for (const Method& method : userMethods) {
        qDebug() << "Method Name:" << method.methodName;
        qDebug() << "Created By:" << method.createdBy;
        qDebug() << "Light Intensity:" << method.lightIntensity;
        qDebug() << "Integration Time:" << method.integrationTime;
        qDebug() << "Integration Count:" << method.integrationCount;
        qDebug() << "Displacement:" << method.displacement;
    }
}

//通过方法名删除方法
void testDeleteMethod() {
    MethodManager methodManager;

    QString methodName = "Method1";

    bool success = methodManager.deleteMethod(methodName);
    if (success) {
        qDebug() << "Method deleted successfully";
    } else {
        qDebug() << "Failed to delete method";
    }
}

//通过方法名查询方法是否存在
void testCheckMethodExists() {
    MethodManager methodManager;

    QString methodName = "Method1";

    bool exists = methodManager.checkMethodExists(methodName);
    if (exists) {
        qDebug() << "Method exists";
    } else {
        qDebug() << "Method does not exist";
    }
}


// 模拟电池数据变化
// QProgressBar *progressBar = m.ui->progressBar;
// testBatteryData(progressBar,66);
void testBatteryData(QProgressBar *progressBar,int value)
{
    progressBar->setValue(value);
}

#endif // TESTFUN_H
